<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .ad-content-form {
    margin-bottom: -10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
  .send-message-btn {
    margin-bottom: 10px;
    margin-top: -46px;
    float: right;
  }
</style>
<template>
  <div>
    广告内容
    <!-- <div class='header-font'>
      <Icon type="md-apps" />
      <span>内容管理广告内容</span>
    </div>
    <div class="line"></div>
    <ad-content-form
      @on-success-valid="handleSubmit"
      class="ad-content-form"
      >
    </ad-content-form>
    <Button
      icon="md-add"
      type="primary"
      @click="handleCreate">
      创建内容
    </Button>
    <ad-content-drawer :modalState="templateState" @on-cancel="cancelTemplate" @ad-content-valid="handleEdit" :row="row"></ad-content-drawer>
    <view-ad-drawer :modalState="modalState" @on-cancel="cancel" :row="row"></view-ad-drawer>
    <edit-confirm-modal :modalState="confirmModal" @cancel-confirm="cancelConfirm" @confirm-valid="handleConfirm"></edit-confirm-modal>
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      border>
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page> -->
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import TablePage from '_c/table-page'
import AdContentForm from '_c/ad-content-form'
import ViewAdDrawer from '_c/view-ad-drawer'
import AdContentDrawer from '_c/ad-content-drawer'
import EditConfirmModal from '_c/edit-confirm-modal'
export default {
  components: {
    AdContentForm,
    ViewAdDrawer,
    AdContentDrawer,
    EditConfirmModal,
    TablePage
  },
  data () {
    return {
      page: 1,
      title: '内容管理广告内容',
      confirmData: {},
      editData: null,
      playload: null,
      rows: 20,
      loading: true,
      modalState: false,
      templateState: false,
      confirmModal: false,
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '创建日期',
          key: 'ctime',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '标题',
          key: 'title',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '操作',
          key: 'cz',
          minWidth: 120,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleClick(params.row.id, params.row)
                  }
                }
              }, '查看'),
              h('Button', {
                props: {
                  type: 'error',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleDelete(params.row.id)
                  }
                }
              }, '删除')
            ])
          }
        }
      ]
    }
  },
  computed: {
    ...mapState({
      adContentList: state => state.adContent.adContentList,
      dataCount: state => state.adContent.adContentDataCount,
      historyData: state => state.adContent.adContentHistoryData,
      pageSize: state => state.adContent.adContentPageSize,
      row: state => state.adContent.row
    })
  },
  methods: {
    ...mapActions([
      'getAdContentList',
      'getAdContentCurrentRow',
      'editAdContentData',
      'changeAdContentPage',
      'changeAdContentPageSize'
    ]),
    handleSubmit (playload) {
      this.playload = playload
      this.getAdContentList({
        ...playload,
        rows: this.rows
      })
    },
    handleClick (id, row) {
      let newRow = {}
      this.id = id
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getAdContentCurrentRow(newRow)
      this.modalState = true
    },
    handleEdit (playload) {
      this.editData = playload
      this.confirmModal = true
      this.templateState = false
    },
    cancel () {
      this.modalState = false
    },
    cancelTemplate (value) {
      this.templateState = false
      value.resetFields()
    },
    cancelConfirm () {
      this.confirmModal = false
      this.templateState = true
    },
    handleConfirm () {
      this.confirmData = {
        title: this.editData.title,
        img: this.editData.img,
        state: this.editData.state
      }
      this.editAdContentData(this.confirmData).then(
        setTimeout(() => {
          this.getAdContentList({
            page: this.page,
            rows: this.rows,
            ...this.playload
          })
        }, 100)
      )
      this.editData.drawer.resetFields()
      this.confirmModal = false
    },
    handleCreate () {
      this.templateState = true
    },
    handleDelete () {
    },
    changePage (index) {
      this.page = index
      this.changeAdContentPage(index)
      this.getAdContentList({
        page: index,
        rows: this.rows,
        ...this.playload
      })
    },
    changePageSize (value) {
      this.rows = value
      this.changeAdContentPageSize(value)
      this.changePage(this.page)
      this.getAdContentList({
        rows: value,
        ...this.playload
      })
    }
  },
  created () {
    this.getAdContentList({})
  }
}
</script>
